<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>corr</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : April 1993</div>
    <p>
      <b>corr</b> -  correlation, covariance</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[cov,Mean]=corr(x,[y],nlags)  </tt>
      </dd>
      <dd>
        <tt>[cov,Mean]=corr('fft',xmacro,[ymacro],n,sect)  </tt>
      </dd>
      <dd>
        <tt></tt>
      </dd>
      <dd>
        <tt>[w,xu]=corr('updt',x1,[y1],w0)  </tt>
      </dd>
      <dd>
        <tt>[w,xu]=corr('updt',x2,[y2],w,xu)  </tt>
      </dd>
      <dd>
        <tt> ...  </tt>
      </dd>
      <dd>
        <tt>[wk]=corr('updt',xk,[yk],w,xu)  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>x</b>
        </tt>: a real vector</li>
      <li>
        <tt>
          <b>y</b>
        </tt>: a real vector, default value x.</li>
      <li>
        <tt>
          <b>nlags</b>
        </tt>: integer, number of correlation coefficients desired.</li>
      <li>
        <tt>
          <b>xmacro</b>
        </tt>: a scilab external (see below).</li>
      <li>
        <tt>
          <b>ymacro</b>
        </tt>: a scilab external (see below), default value xmacro</li>
      <li>
        <tt>
          <b>n</b>
        </tt>: an integer, total size of the sequence (see below).</li>
      <li>
        <tt>
          <b>sect</b>
        </tt>: size of sections of the sequence (see below).</li>
      <li>
        <tt>
          <b>xi</b>
        </tt>: a real vector</li>
      <li>
        <tt>
          <b>yi</b>
        </tt>: a real vector,default value xi.</li>
      <li>
        <tt>
          <b>cov</b>
        </tt>: real vector, the correlation coefficients</li>
      <li>
        <tt>
          <b>Mean</b>
        </tt>: real number or vector,  the mean of x and if given y</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
    Computes
  </p>
    <pre>

                n - m 
                 ====
                 \                                                 1
        cov(m) =  &gt;        (x(k)  - xmean) (y(m+k)      - ymean) * ---
                 /                                                  n
                 ====
                 k = 1
   
    </pre>
    <p>
    for   m=0,..,<tt>
        <b>nlag-1</b>
      </tt> and two vectors <tt>
        <b>x=[x(1),..,x(n)]</b>
      </tt>
      <tt>
        <b>y=[y(1),..,y(n)]</b>
      </tt>
    </p>
    <p>
    Note that if x and y sequences are differents corr(x,y,...) is
    different with corr(y,x,...)
  </p>
    <dl>
      <dd>
        <b>Short sequences</b>
        <tt>
          <b>[cov,Mean]=corr(x,[y],nlags)</b>
        </tt> returns the first nlags
    correlation coefficients and Mean = <tt>
          <b>mean(x)</b>
        </tt>
    (mean of <tt>
          <b>[x,y]</b>
        </tt> if <tt>
          <b>y</b>
        </tt> is an argument).
    The sequence <tt>
          <b>x</b>
        </tt> (resp. <tt>
          <b>y</b>
        </tt>) is assumed real, and <tt>
          <b>x</b>
        </tt> 
    and <tt>
          <b>y</b>
        </tt> are of same dimension n.</dd>
      <dd>
        <b>Long sequences</b>
        <tt>
          <b>[cov,Mean]=corr('fft',xmacro,[ymacro],n,sect)</b>
        </tt> Here <tt>
          <b>xmacro</b>
        </tt> is either<dl>
          <dd>
            <b></b>
            <p> 
        a function of type <tt>
                <b>[xx]=xmacro(sect,istart)</b>
              </tt> which
        returns a vector <tt>
                <b>xx</b>
              </tt> of dimension
        <tt>
                <b>nsect</b>
              </tt> containing the part of the sequence with
        indices from <tt>
                <b>istart</b>
              </tt> to
        <tt>
                <b>istart+sect-1</b>
              </tt>.</p>
          </dd>
          <dd>
            <b></b>
            <p>
        a fortran subroutine or C procedure which performs the same
        calculation. (See the source code of <tt>
                <b>dgetx</b>
              </tt> for an
        example). <tt>
                <b>n</b>
              </tt> = total size of the
        sequence. <tt>
                <b>sect</b>
              </tt> = size of sections of the
        sequence. <tt>
                <b>sect</b>
              </tt> must be a power of
        2. <tt>
                <b>cov</b>
              </tt> has dimension
        <tt>
                <b>sect</b>
              </tt>. Calculation is performed by FFT.</p>
          </dd>
        </dl>
      </dd>
      <dd>
        <b>Updating method</b>
        <pre>

    [w,xu]=corr('updt',x1,[y1],w0)
    [w,xu]=corr('updt',x2,[y2],w,xu)
     ...
    wk=corr('updt',xk,[yk],w,xu)
    
        </pre>
        <p>
    With this calling sequence the calculation is updated at each
    call to <tt>
            <b>corr</b>
          </tt>.</p>
        <pre>

    w0 = 0*ones(1,2*nlags);
    nlags = power of 2.
    
        </pre>
        <p>
          <tt>
            <b>x1,x2,...</b>
          </tt> are parts of <tt>
            <b>x</b>
          </tt> such that
    <tt>
            <b>x=[x1,x2,...]</b>
          </tt> and sizes of <tt>
            <b>xi</b>
          </tt> a power of
    2.  To get <tt>
            <b>nlags</b>
          </tt> coefficients a final fft must be
    performed <tt>
            <b>c=fft(w,1)/n</b>
          </tt>; <tt>
            <b>cov=c(1nlags)</b>
          </tt>
    (<tt>
            <b>n</b>
          </tt> is the size of <tt>
            <b>x (y)</b>
          </tt>).  Caution: this
    calling sequence assumes that <tt>
            <b>xmean = ymean = 0</b>
          </tt>.</p>
      </dd>
    </dl>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

x=%pi/10:%pi/10:102.4*%pi;
rand('seed');rand('normal');
y=[.8*sin(x)+.8*sin(2*x)+rand(x);.8*sin(x)+.8*sin(1.99*x)+rand(x)];
c=[];
for j=1:2,for k=1:2,c=[c;corr(y(k,:),y(j,:),64)];end;end;
c=matrix(c,2,128);cov=[];
for j=1:64,cov=[cov;c(:,(j-1)*2+1:2*j)];end;
rand('unif')
//
rand('normal');x=rand(1,256);y=-x;
deff('[z]=xx(inc,is)','z=x(is:is+inc-1)');
deff('[z]=yy(inc,is)','z=y(is:is+inc-1)');
[c,mxy]=corr(x,y,32);
x=x-mxy(1)*ones(x);y=y-mxy(2)*ones(y);  //centring
c1=corr(x,y,32);c2=corr(x,32);
norm(c1+c2,1)
[c3,m3]=corr('fft',xx,yy,256,32);
norm(c1-c3,1)
[c4,m4]=corr('fft',xx,256,32);
norm(m3,1),norm(m4,1)
norm(c3-c1,1),norm(c4-c2,1)
x1=x(1:128);x2=x(129:256);
y1=y(1:128);y2=y(129:256);
w0=0*ones(1:64);   //32 coeffs
[w1,xu]=corr('u',x1,y1,w0);w2=corr('u',x2,y2,w1,xu);
zz=real(fft(w2,1))/256;c5=zz(1:32);
norm(c5-c1,1)
[w1,xu]=corr('u',x1,w0);w2=corr('u',x2,w1,xu);
zz=real(fft(w2,1))/256;c6=zz(1:32);
norm(c6-c2,1)
rand('unif')
// test for Fortran or C external 
//
deff('[y]=xmacro(sec,ist)','y=sin(ist:(ist+sec-1))');
x=xmacro(100,1);
[cc1,mm1]=corr(x,2^3);
[cc,mm]=corr('fft',xmacro,100,2^3);
[cc2,mm2]=corr('fft','corexx',100,2^3);
[maxi(abs(cc-cc1)),maxi(abs(mm-mm1)),maxi(abs(cc-cc2)),maxi(abs(mm-mm2))]

deff('[y]=ymacro(sec,ist)','y=cos(ist:(ist+sec-1))');
y=ymacro(100,1);
[cc1,mm1]=corr(x,y,2^3);
[cc,mm]=corr('fft',xmacro,ymacro,100,2^3);
[cc2,mm2]=corr('fft','corexx','corexy',100,2^3);
[maxi(abs(cc-cc1)),maxi(abs(mm-mm1)),maxi(abs(cc-cc2)),maxi(abs(mm-mm2))]

 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="fft.htm">
        <tt>
          <b>fft</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
  </body>
</html>
